package Utils;

import beans.ResponseData;
import cn.hutool.json.JSONUtil;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class JSONUtils {
    private JSONUtils(){}
    public static <T> String result(Enum e , T data){
        ResponseData<T> responseData = new ResponseData<T>();
        responseData.setData(data);
        try {
            Method getCode = e.getClass().getMethod("getCode");
            Method getMsg = e.getClass().getMethod("getMsg");
            if (getCode != null && getMsg != null) {
                responseData.setMsg((String) getMsg.invoke(e));
                responseData.setCode((Integer) getCode.invoke(e));
//                return JSON.toJSONString(responseData);
                return JSONUtil.toJsonStr(responseData);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
